//
//  YTMyPersonEdit.m
//  qiyunapp
//
//  Created by qiyun on 2018/4/17.
//  Copyright © 2018年 qiyun. All rights reserved.
//

#import "YTMyPersonEdit.h"
@interface YTMyPersonEdit ()<UITextFieldDelegate>
{
    UITextField * _nameField;
    UITextField * _sfzField;
    UIButton*_saveBtn;
}

@property (nonatomic, strong) NSArray * titleArray;
@property (nonatomic, strong) NSArray * desArray;


@end

@implementation YTMyPersonEdit
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //判断登录状态
    NSMutableDictionary *data = [Tookit getUserCasheInfo];
    NSString*token= [data objectForKey:@"token"];
  
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=RGBA(236, 236, 236);
    [self setNav];
    [self setUI];
    
}
-(void)setNav{
    self.titleLabel.text=@"";
    self.lineView.hidden=YES;
    [self.leftimaBtn setImage:IMAGE(@"Mine_back") forState:UIControlStateNormal];
    
}
-(void)setUI{
    _nameField=[[UITextField alloc]init];
    _nameField.delegate=self;
    _nameField.backgroundColor=[UIColor whiteColor];
    _nameField.borderStyle = UITextBorderStyleRoundedRect;//圆角
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; // 创建属性字典
    attrs[NSFontAttributeName] =KFONT(16); // 设置font
    attrs[NSForegroundColorAttributeName] = RGBA(153,153,153); // 设置颜色
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:@"请输入姓名" attributes:attrs]; // 初始化富文本占位字符串
    _nameField.attributedPlaceholder = attStr;
    [self.view addSubview:_nameField];

    _sfzField=[[UITextField alloc]init];
    _sfzField.delegate=self;
    _sfzField.borderStyle = UITextBorderStyleRoundedRect;//圆角
    NSMutableDictionary *attr = [NSMutableDictionary dictionary]; // 创建属性字典
    attr[NSFontAttributeName] =KFONT(16); // 设置font
    attr[NSForegroundColorAttributeName] = RGBA(153,153,153); // 设置颜色
    NSAttributedString *att = [[NSAttributedString alloc] initWithString:@"请输入身份证" attributes:attr]; // 初始化富文本占位字符串
    _sfzField.attributedPlaceholder = att;
    [self.view addSubview:_sfzField];
    
    _nameField.frame=CGRectMake(AfW(20), k_statusbarHeight+k_NavigationHeight, SCREEN_W-2*AfW(20), 44);
    _sfzField.frame=CGRectMake(AfW(20), CGRectGetMaxY(  _nameField.frame)+10, SCREEN_W-2*AfW(20), 44);
    
    
    //保存按钮
    _saveBtn=[[UIButton alloc]init];
    [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [_saveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];

    [_saveBtn setBackgroundColor:RGBA(234, 49, 47)];
    [_saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_saveBtn.layer setCornerRadius:3];
    _saveBtn.layer.masksToBounds = YES;
    [self.view addSubview:_saveBtn];
    _saveBtn.frame=CGRectMake(AfW(20), SCREEN_H-100, SCREEN_W-2*AfW(20), 44);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:1];
}
-(void)save{
    if (_nameField.text.length <=0) {
        PROMPT(@"请输入姓名");
        return;
    }
    //判断身份证
    if ( _sfzField.text.length<=0) {
        PROMPT(@"请输入身份证");
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSMutableDictionary *data = [Tookit getUserCasheInfo];
    NSString*token= [data objectForKey:@"token"];
    
    NSDictionary *dicts = @{@"certNo":_sfzField.text,
                            @"realName":_nameField.text,
                            @"token":token
                            };
    __weak typeof(self) weakSelf = self;
    [SendRequest getinfoDetailWithURL:k_MainUrl(k_User_nameAndsfz) withDictionary:dicts result:^(NSDictionary *result, NSError *error) {
        NSLog(@"获取---%@",result);
        if ([result[@"success"] isEqual:@1] ) {
            PROMPT(result[@"msg"]);
            
            NSString* certNo= [NSString stringWithFormat:@"%@******%@", [_sfzField.text substringToIndex:1], [_sfzField.text substringFromIndex:14]];
            //数据存本地
            
            NSMutableDictionary *data = [Tookit getUserCasheInfo];
            NSString*name= [data objectForKey:@"name"];
            name=_nameField.text;
            [data setValue:name forKey:@"name"];
            
            NSString*cert= [data objectForKey:@"certNo"];
            cert=certNo;
            [data setValue:cert forKey:@"certNo"];
            
            [Tookit saveUserCasheInfo:[NSMutableDictionary dictionaryWithDictionary:data]];
            
            NSMutableDictionary *datatest = [Tookit getUserCasheInfo];
            
            [self.navigationController popViewControllerAnimated:1];
            
             PROMPT(result[@"msg"]);
        }else{
            PROMPT(result[@"msg"]);
        }
         [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];
    
}

@end

